<?php

declare(strict_types=1);

namespace EasyHttp;


class Request
{
    
    private static $types=[
        'json'=>'application/json',
        'urlencode'=>'application/x-www-form-urlencoded',
        'form'=>'multipart/form-data',
        'stream'=>'application/octet-stream',
        'text'=>'text/plain',
        'html'=>'text/html',
        'xml'=>'text/xml',
        'csv'=>'text/csv',
        'pdf'=>'application/pdf',
        'zip'=>'application/zip',
        'gzip'=>'application/gzip',
        'tar'=>'application/x-tar',
        'rar'=>'application/x-rar-compressed',
        '7z'=>'application/x-7z-compressed',
        'bz2'=>'application/x-bzip2',
        'gz'=>'application/gzip',
    ];
  

    /**
     * get请求
     *
     * @param string $uri
     * @param array $body
     * @param array $header
     * @param string $type
     * @return mixed
     */
    public static function get(string $uri, array $body = [], array $header,string $type)
    {

        return self::REST('get', $uri, $body,$header,$type);
    }
    /**
     * post请求
     *
     * @param string $uri
     * @param array $body
    * @param array $header
     * @param string $type
     * @return mixed
     */
    public static function post(string $uri, array $body = [], array $header,string $type)
    {

        return self::REST('post', $uri, $body, $header,$type);
    }
    /**
     * delete请求
     *
     * @param string $uri
     * @param array $body
      * @param array $header
     * @param string $type
     * @return mixed
     */
    public static function delete(string $uri, array $body = [], array $header,string $type)
    {

        return self::REST('delete', $uri, $body, $header,$type);
    }
    /**
     * put请求
     *
     * @param string $uri
     * @param array $body
     * @param array $header
     * @param string $type
     * @return mixed
     */
    public static function put(string $uri, array $body, array $header,string $type)
    {

        return self::REST('put', $uri, $body,$header,$type);
    }
    /**
     * REST请求
     *
     * @param string $method
     * @param string $uri
     * @param array $body
     * @param array $header
     * @param string $type
     * @return mixed
     */
    public static function REST(string $method, string $uri, array $body = [], array $header,string $type)
    {

        return self::run($method, $uri, $body,$header,$type);
    }
    /**
     * HTTP请求
     *
     * @param string $method
     * @param string $uri
     * @param array $body
     * @param array $header
     * @param string $type
     * @return mixed
     */
    public static function run(string $method, string $uri, array $body = [], array $header,string $type,int $timeOut=30)
    {
       
     
        $method = strtoupper($method);
        $ContentType=self::$types[$type];
        $header[]='Content-Type: '.$ContentType;
        switch ($type) {
            case 'json':
                $body = empty($body) ? '' : json_encode($body);
                break;
             case 'urlencode':
                $body = empty($body) ? '' : http_build_query($body);
                break;
            default:
                $body = empty($body) ? '' : $body;
               
                break;
        }
        $ch = curl_init();
        curl_setopt_array($ch,[
            CURLOPT_HTTPHEADER=>$header,
            CURLOPT_RETURNTRANSFER=>true,
            CURLOPT_HEADER=>false,
            CURLOPT_URL=>$uri,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_TIMEOUT=>$timeOut,
            CURLOPT_CUSTOMREQUEST=>$method,
            CURLOPT_POSTFIELDS=>$body
        ]);
        
        $res = curl_exec($ch);
        curl_close($ch);
        return $res;
    }
    /**
     * 自定义请求
     *
     * @param array $options
     * @return mixed
     */
    public static function send(array $options)
    {


        $curl = curl_init();
       
        curl_setopt_array($curl,$options);

        $response = curl_exec($curl);

        curl_close($curl);
        
        return $response;

    }
    public static function joinParams(string $uri, array $params): string
    {
        return $uri . '?' . http_build_query($params);
    }
}
